package com.qianfeng.smsplatform.webmaster.controller;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.github.pagehelper.PageInfo;
import com.qianfeng.smsplatform.webmaster.annotations.IgnoreLog;
import com.qianfeng.smsplatform.webmaster.annotations.LogOperationAnno;
import com.qianfeng.smsplatform.webmaster.dto.R;
import com.qianfeng.smsplatform.webmaster.pojo.TAdminUser;
import com.qianfeng.smsplatform.webmaster.pojo.TBlackList;
import com.qianfeng.smsplatform.webmaster.service.BlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by Jackiechan on 2021/10/25/16:53
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/black")
@Api(tags = "黑名单管理")
public class BalckListController {

    private BlackListService blackListService;


    @Autowired
    public void setBlackListService(BlackListService blackListService) {
        this.blackListService = blackListService;
    }

    @IgnoreLog//当前注解是一个自定义注解,没有任何实际作用,就是为了区分是不是需要记录日志
    @GetMapping("/black/{id}")
    public R findBlackById(@PathVariable  Long id) {
        TBlackList blackList = blackListService.findBlackById(id);
        return R.setOK(blackList);
    }

    @PostMapping("/black/{mobile}")
    public R addBlack(@PathVariable String mobile, @ApiIgnore HttpSession session) {
        TBlackList tBlackList = new TBlackList();
        tBlackList.setMobile(mobile);
        Object user = session.getAttribute("user");
        if (user != null) {
            Long id = ((TAdminUser) user).getId();//创建者的 id
            Long type = ((TAdminUser) user).getType();//创建者类型
            tBlackList.setCreater(id);
            tBlackList.setOwntype(type);
        }
        blackListService.addBlack(tBlackList);

        return R.setOK();
    }

    @GetMapping("/all")
    @ApiOperation("分页查询所有数据")
    @LogOperationAnno("分页查询黑名单的数据")
    public R findAllBlackByPage(@RequestParam(defaultValue = "1") @ApiParam(name ="pageNum",value = "页码") int pageNum, @RequestParam(defaultValue = "10") @ApiParam(name ="pageSize",value = "每页条数")  int pageSize) {
        PageInfo<TBlackList> pageInfo = blackListService.findAll(pageNum, pageSize);
        return R.setOK(pageInfo);
    }

    @PostMapping("/black")
    public R updateBlackById(@RequestBody TBlackList blackList, @ApiIgnore  HttpSession session) {
        Object user = session.getAttribute("user");
        if (user != null) {
            Long id = ((TAdminUser) user).getId();//获取到当前登陆的用户
            Long type = ((TAdminUser) user).getType();//获取到当前用户的类型
            blackList.setCreater(id);
            blackList.setOwntype(type);

        }
        blackListService.updateBlack(blackList);
        return R.setOK();
    }

    @DeleteMapping("/black")
    public R deleteByIds(@RequestBody Map map) {
        System.err.println(map);
        blackListService.deleteBlackByIds((List<Integer>) map.get("ids"));
        return R.setOK();
    }
}
